// pages/components/timepicker.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    uname: {
      type: String,
      value: 'start'
    },
    type: {
      type: String,
      value: 'start'
    },
    dvalue: {
      type: Array,
      value: [],
      observer() {
        this.init();
      }
    },
    pos: {
      type: Number,
      default: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    timePicker: [],
    selectItem: [0, 0]
  },

  lifetimes: {
    attached() {
     this.init();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      let {dvalue, type} = this.data;
      let selectItem = dvalue && dvalue.length
        ? dvalue
        : type === 'end' ? [24, 0] : [0, 0]
        console.log(dvalue)
      this.setData({
        selectItem,
        timePicker: [
          Array.from({
            length: 25
          }, (_, index) => String(index).padStart(2, '0')),
          selectItem[0] === 24 ?
            ['00'] : Array.from({
            length: 60
          }, (_, index) => String(index).padStart(2, '0'))
        ]
      })
    },
    handleChange(e) {
      let value = e.detail.value;
      this.setData({
        selectItem: value
      });
      const {timePicker, type, pos} = this.data;
      this.triggerEvent('timeevent', {
        pos,
        type,
        value: `${
          timePicker[0][value[0]]
        }:${
          timePicker[1][value[1]]
        }`
      })
    },
    handleColumnStart(e) {
      let {
        column,
        value
      } = e.detail;
      if (column === 0) {
        this.setData({
          timePicker: [
            Array.from({
              length: 25
            }, (_, index) => String(index).padStart(2, '0')),
            value === 24 ?
            ['00'] :
            Array.from({
              length: 60
            }, (_, index) => String(index).padStart(2, '0'))
          ]
        })
      }
    }
  }
})